﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    //
    public class YCAEK03DOCData : BaseFunction
    {
        ApplicationBase app
        {
            get

            {
                return ApplicationBase.Instance;
            }
        }
        public INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }

        }
      
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public YCAEK03DOCData()
            : base()
        {
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day); 
        } 
        public DataSet LoadEnv()
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwData = INTEXDB.LIEFDAT.NewLIEFDATRow();
            dwData["LSMANDANT"] = "00";
            SResult = INTEXDBC.QueryLIEFDAT(dwData, 6);
            SReturn.Merge(SResult);
            dwData = INTEXDB.SDDAT.NewRow();
            dwData["SDSA"] = "0255";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            return SReturn;
        }
        public DataSet GetOrderBySupplier(DataRow dwInput)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwData = INTEXDB.EKKOPF.NewRow();
            dwData["EKKMANDANT"] = "00";
            dwData["EKKLIEFNR"] = dwInput["LSLIEFNR"].ToString();
            SResult = INTEXDBC.QueryEKKOPF(dwData, 3);
            SReturn.Merge(SResult);
            return SReturn;
        }
        public void EKDelete(string client, string krs, string lfd, string pos = "000")
        {
            try
            { 
                DataSet setResult = INTEXDBC.QueryEKPOS(client, krs, lfd);
                if (!setResult.Tables.Contains("EKPOS"))
                    return;
                int count = setResult.Tables["EKPOS"].Rows.Count;
                int cdel = 0;

                var query = from DataRow row in setResult.Tables["EKPOS"].AsEnumerable()
                            select row;
                if (pos == "000")
                {
                    foreach (DataRow row in query)
                    {
                        decimal EKPME3 = System.Convert.ToDecimal(row["EKPME3"].ToString());
                        string EKPBEKZ = row["EKPBEKZ"].ToString();
                        string EKPKNTRNRL = row["EKPKNTRNRL"].ToString();
                        if (EKPBEKZ.Trim() != "")
                        {
                            MessageBox.Show("采购单数量已经确认，不能删除!");
                            return;
                        }
                        if (EKPKNTRNRL.Trim() != "")
                        {
                            MessageBox.Show("采购单已经审核，不能删除!");
                            return;
                        }
                        if (EKPME3 > 0)
                        {
                            MessageBox.Show("该采购单已经入库，不能删除!");
                            return;
                        }
                    }
                }
                if (pos != "000")
                {
                    query = from DataRow row in setResult.Tables["EKPOS"].AsEnumerable()
                            where row["EKPAUFPOS"].ToString() == pos
                            select row;
                    foreach (DataRow row in query)
                    {
                        decimal EKPME3 = System.Convert.ToDecimal(row["EKPME3"].ToString());

                        if (EKPME3 > 0)
                        {
                            if (MessageBox.Show("需要继续单行删除，请确认", "该采购单已经入库，不能删除!", MessageBoxButton.YesNo) == MessageBoxResult.No)
                            {
                                return;
                            }
                        }
                    }
                }
                foreach (DataRow row in setResult.Tables["EKPOS"].Rows)
                {

                    string posh = row["EKPAUFPOS"].ToString();
                    if (pos != "000")
                    {
                        if (posh != pos)
                            continue;
                    }
                    decimal EKPME3 = System.Convert.ToDecimal(row["EKPME3"].ToString());
                    if (EKPME3 > 0)
                    {
                        MessageBox.Show("该采购单已经入库，不能删除!");
                        continue;
                    }
                    INTEXDBC.DeleteEKPOS(client, krs, lfd, posh);
                    cdel++;
                    INTEXDBC.UpdateICC_ABAUFM("EK", client, krs, lfd, posh);
                }
                if (cdel == count)
                {
                    INTEXDBC.DeleteEKKOPF(client, krs, lfd);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public DataSet ValidateEK(DataRow dwInput, ref bool IsValid)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            string client = dwInput["EKPMANDANT"].ToString();
            string krs = dwInput["EKPAUFKRS"].ToString();
            string lfd = dwInput["EKPAUFLFD"].ToString();
            DataRow dwData = INTEXDB.EKKOPF.NewRow();
            dwData["EKKMANDANT"] = client;
            dwData["EKKAUFKRS"] = krs;
            dwData["EKKAUFLFD"] = lfd;
            Result = INTEXDBC.QueryEKKOPF(dwData);
            Return.Merge(Result);
            if (!Return.Tables.Contains("EKKOPF"))
            {
           
                MessageBox.Show("采购单" + krs + lfd + " 无效，强制删除,请立即联系程序员");
                return Return;
                EKDelete("00", krs, lfd, "000");
            }
            if (Return.Tables["EKKOPF"].Rows.Count == 0)
            {
                
                MessageBox.Show("采购单" + krs + lfd + " 无效，强制删除,请立即联系程序员");
                return Return;
                EKDelete("00", krs, lfd, "000");
            }
            IsValid = true;
            return Return;
        }
        public DataSet BuildData(DataRow dwInput,int option=1)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow rowtemp = null;        
            try
            {
                bool IsValid = false;
                Result = ValidateEK(dwInput, ref IsValid);
                if (!IsValid)
                {
                    return Return;                   
                }
                Return.Merge(Result);
                #region=======Order======
                foreach (DataRow row in Return.Tables["EKKOPF"].Rows)
                {
                    rowtemp = INTEXDB.EKKOPF.NewRow();
                    rowtemp["EKKMANDANT"] = row["EKKMANDANT"];
                    rowtemp["EKKAUFKRS"] = row["EKKAUFKRS"];
                    rowtemp["EKKAUFLFD"] = row["EKKAUFLFD"];
                    Result = INTEXDBC.QueryLIEFDAT(rowtemp, 5);
                    Return.Merge(Result);
                    Result = INTEXDBC.QueryLIEFKO(rowtemp, 5);
                    Return.Merge(Result);
                    Result = INTEXDBC.QueryWAPROF(rowtemp, 5);
                    Return.Merge(Result);
                    rowtemp = INTEXDB.EKPOS.NewRow();
                    rowtemp["EKPMANDANT"] = row["EKKMANDANT"];
                    rowtemp["EKPAUFKRS"] = row["EKKAUFKRS"];
                    rowtemp["EKPAUFLFD"] = row["EKKAUFLFD"];
                    rowtemp["EKPBEKZ"] = dwInput["EKPBEKZ"];
                    rowtemp["EKPKNTRNRL"] = dwInput["EKPKNTRNRL"];
                    if (option == 2)
                    {
                        Result = INTEXDBC.QueryICC_EKPOS(rowtemp, 9);
                    }
                    else
                    {
                        Result = INTEXDBC.QueryICC_EKPOS(rowtemp, 3);
                    }
                    Return.Merge(Result);
                    rowtemp = INTEXDB.ICC_ABAUFM.NewRow();
                    rowtemp["ABAMANDANT"] = row["EKKMANDANT"];
                    rowtemp["ABAAUFKRS"] = row["EKKAUFKRS"];
                    rowtemp["ABAAUFLFD"] = row["EKKAUFLFD"];
                    Result = INTEXDBC.QueryICC_ABAUFM(rowtemp, 4);
                    Return.Merge(Result);
                    rowtemp = INTEXDB.ICC_ABAUFM.NewRow();
                    rowtemp["ABAMANDANT"] = row["EKKMANDANT"];
                    rowtemp["ABAAUFKRS"] = row["EKKAUFKRS"];
                    rowtemp["ABAAUFLFD"] = row["EKKAUFLFD"];
                    Result = INTEXDBC.QueryICC_ABAUFM(rowtemp, 1);
                    if (Result.Tables.Count > 0)
                    {
                        Result.Tables[0].TableName = "ABPOS";
                        Return.Merge(Result);
                    }
                    rowtemp = INTEXDB.ICC_ABAUFM.NewRow();
                    rowtemp["ABAMANDANT"] = row["EKKMANDANT"];
                    rowtemp["ABAAUFKRS"] = row["EKKAUFKRS"];
                    rowtemp["ABAAUFLFD"] = row["EKKAUFLFD"];
                    Result = INTEXDBC.QueryICC_ABAUFM(rowtemp, 21);
                    if (Result.Tables.Count > 0)
                    {
                        Result.Tables[0].TableName = "MAINFABRIC";
                        Return.Merge(Result);
                    }
                    Result = INTEXDBC.QueryICC_ABAUFM(rowtemp, 31);
                    if (Result.Tables.Count > 0)
                    {
                        Result.Tables[0].TableName = "ABMATRIX";
                        Return.Merge(Result);
                    }
                    rowtemp = INTEXDB.EKVRECH.NewRow();
                    rowtemp["EKVMANDANT"] = row["EKKMANDANT"];
                    rowtemp["EKVRECHNR"] = row["EKKAUFKRS"].ToString() + "-" + row["EKKAUFLFD"].ToString();
                    Result = INTEXDBC.QueryEKVRECH(rowtemp, 2);
                    Return.Merge(Result);
                }
                #endregion
                #region========material==========
                /*var query = Return.Tables["ICC_EKPOS"].AsEnumerable()
                        .Select(row => new
                        {
                            ARTG = row.Field<string>("EKPARTGR"),
                            ART = row.Field<string>("EKPART"),
                            VAR = row.Field<string>("EKPVAR"),
                            COLOR = row.Field<string>("EKPFARBE"),
                            SIZE = row.Field<string>("EKPGROE")
                        }).Distinct();*/
                var query = Return.Tables["ICC_EKPOS"].AsEnumerable()
        .Select(row => new
        {
            ARTG = row.Field<string>("EKPARTGR"),
            ART = row.Field<string>("EKPART"),
            VAR = row.Field<string>("EKPVAR")
        }).Distinct();
                foreach (var row in query)
                {
                    switch (row.ARTG)
                    {
                        case "1":
                            break;
                        case "2":
                            break;
                        case "3":
                            rowtemp = INTEXDB.WASTAMM.NewRow();
                            rowtemp["WAMANDANT"] = "00";
                            rowtemp["WAART"] = row.ART;
                            rowtemp["WAVAR"] = Left(row.VAR, 2);
                            Result = INTEXDBC.QueryWASTAMM(rowtemp, 2);
                            Return.Merge(Result);
                            rowtemp = INTEXDB.STUELI.NewRow();
                            rowtemp["STUELIMANDANT"] = "00";
                            rowtemp["STUELISAPD"] = row.ART;
                            Result = INTEXDBC.QuerySTUELI(rowtemp, 3);
                            Return.Merge(Result);
                            //FSSTAMM;FSFARBEN;FSSTUELI;FSGROE
                            rowtemp = INTEXDB.WAFARBEN.NewRow();
                            rowtemp["WAFMANDANT"] = "00";
                            rowtemp["WAFART"] = row.ART;
                            rowtemp["WAFVAR"] = Left(row.VAR, 2);
                            Result = INTEXDBC.QueryWAFARBEN(rowtemp, 2);
                            Return.Merge(Result);
                            break;
                        case "4":
                            break;
                        case "5":
                            rowtemp = INTEXDB.FSSTAMM.NewRow();
                            rowtemp["FSMANDANT"] = "00";
                            rowtemp["FSART"] = row.ART;
                            rowtemp["FSVAR"] = Left(row.VAR, 2);
                            Result = INTEXDBC.QueryFSSTAMM(rowtemp, 1);
                            Return.Merge(Result);
                            //FSSTAMM;FSFARBEN;FSSTUELI;FSGROE
                            rowtemp = INTEXDB.FSFARBEN.NewRow();
                            rowtemp["FSFMANDANT"] = "00";
                            rowtemp["FSFART"] = row.ART;
                            rowtemp["FSFVAR"] = Left(row.VAR, 2);
                            Result = INTEXDBC.QueryFSFARBEN(rowtemp, 2);
                            Return.Merge(Result);
                            rowtemp = INTEXDB.FSGROE.NewRow();
                            rowtemp["FSGMANDANT"] = "00";
                            rowtemp["FSGART"] = row.ART;
                            rowtemp["FSGVAR"] = row.VAR;
                            Result = INTEXDBC.QueryFSGROE(rowtemp, 2);
                            Return.Merge(Result);
                            break;
                        case "6":
                            break;
                        case "7":
                            break;
                        case "8":
                            rowtemp = INTEXDB.MSSTAMM.NewRow();
                            rowtemp["MSMANDANT"] = "00";
                            rowtemp["MSMAT"] = Left(row.ART, 14);
                            rowtemp["MSVAR"] = row.VAR;
                            Result = INTEXDBC.QueryMSSTAMM(rowtemp, 1);
                            Return.Merge(Result);
                            break;
                        default:
                            break;
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }


    }
}